import { h, intersection } from "koishi"

export const name = "雪豹闭嘴"

function reply(content) {
  return session => session.send([h.quote(session.messageId), ...h.normalize(content)])
}

function emojiResponse(id, fallbackReply) {
  if (typeof id === "string") id = id.codePointAt(0)
  return async session => (
    await session.onebot?._request?.("set_msg_emoji_like", {
      message_id: session.event.message.id,
      emoji_id: id,
    }) ||
    fallbackReply && reply(fallbackReply)
  )
}

export function apply(ctx) {
  ctx.on("message", session => {
    if (session.userId !== "63360614" && session.content.match(/^(?:command|plugin(?:\s+|\.)(?:install|upgrade|uninstall|i|update|up|r)|timer)(?:\s|$)/i)) {
      shutup(
        ctx.channel(session.channelId).user("3782672067"),
        /^啊？+$/,
        reply("雪豹闭嘴"),
      )
      shutup(
        ctx.channel(session.channelId).user("3934982256"),
        /^权限不足。+$/,
        emojiResponse(326),
      )
    }
    if (session.content.match(/^\/?抽象字体/))
      shutup(
        ctx.channel(session.channelId).user("1732167820"),
        /^📄体/,
        emojiResponse(326),
      )
    if (session.content.match(/^\/?wiki/))
      shutup(
        ctx.channel(session.channelId).user("1732167820"),
        /^额。没有找到wiki内容，换个关键词试试吧～$/,
        emojiResponse(326),
      )
    if (session.content.match(/^\/?sp/))
      shutup(
        ctx.channel(session.channelId).user("1732167820"),
        /^未开启sp功能/,
        emojiResponse(326),
      )
    if (session.content.match(/^\/help\s+\S/))
      shutup(
        ctx.channel(session.channelId).user("1841705081"),
        /^<img /,
        emojiResponse(326),
      )
  })

  ctx.channel().before("send", session => {
    if (session.content.match(/^(?:command|plugin(?:\s+|\.)(?:install|upgrade|uninstall|i|update|up|r))(?:\s|$)/i)) {
      shutup(
        ctx.channel(session.channelId).user("3782672067"),
        /^啊？+$/,
        reply("雪豹闭嘴"),
      )
      shutup(
        ctx.channel(session.channelId).user("3934982256"),
        /^权限不足。+$/,
        emojiResponse(326),
      )
    }
    if (session.content.match(/^\/?抽象字体/))
      shutup(
        ctx.channel(session.channelId).user("1732167820"),
        /^📄体/,
        emojiResponse(326),
      )
    if (session.content.match(/^\/?wiki/))
      shutup(
        ctx.channel(session.channelId).user("1732167820"),
        /^额。没有找到wiki内容，换个关键词试试吧～$/,
        emojiResponse(326),
      )
    if (session.content.match(/^\/?sp/))
      shutup(
        ctx.channel(session.channelId).user("1732167820"),
        /^未开启sp功能/,
        emojiResponse(326),
      )
  })

  shutup(
    ctx.channel().user("1732167820"),
    /^你没有权限使用此命令$/,
    emojiResponse(326),
    true,
  )

  shutup(
    ctx.channel(),
    /^∪∪∪∪/,
    emojiResponse(326),
    true,
  )

  function shutup(ctx, re, cb, always) {
    const off = ctx.middleware(async (session, next) => {
      if (!session.content.match(re)) return next()
      if (!always) off()
      const botIsMod = await session.bot.getGuildMember(session.guildId, session.selfId).then(m => intersection(m.roles, ["owner", "admin"]).length > 0).catch(() => false)
      if (botIsMod) try {
        await session.bot.deleteMessage(session.guildId, session.messageId)
        return
      } catch {}
      cb?.(session)
    })
    if (!always) ctx.setTimeout(off, 10000)
  }
}
